/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.atomojo.app.storage.xmldb;

import java.io.File;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.atomojo.app.Storage;
import org.atomojo.app.StorageFactory;
import org.atomojo.app.db.DB;
import org.infoset.xml.sax.SAXDocumentLoader;
import org.restlet.Context;
import org.restlet.data.Protocol;
import org.restlet.data.Reference;

/**
 *
 * @author alex
 */
public class XMLDBStorageFactory implements StorageFactory {

   Map<String,XMLDBStorage> cache;
   Context context;
   public XMLDBStorageFactory() {
      cache = new TreeMap<String,XMLDBStorage>();
   }
   
   public void init(Context context) {
      this.context = context;
   }

   public Set<Protocol> getStorageProtocols() {
      return Collections.emptySet();
   }


   public Storage getStorage(DB db) 
      throws Exception
   {
      XMLDBStorage storage = cache.get(db.getName());
      if (storage==null) {
         File dbDir = db.getDatabaseDir();
         File xmldbDir = new File(dbDir,"xmldb");
         storage = new XMLDBStorage(xmldbDir,db.getName(),new Reference("riap://component/exist/"+db.getName()+"/"),new SAXDocumentLoader());
         cache.put(db.getName(),storage);
         storage.init(context);
      }
      return storage;
   }
}
